﻿namespace TestLeakApp
{
    using GKQC.Common;
    using System;
    using System.IO.Ports;
    using System.Threading;
    using System.Windows.Forms;

    public class BOX20:IBox
    {
        public delegate void ShowSendDataDel(byte[] array, int len);
        public delegate void MyComDelegate(byte[] dList, int len);
        private SerialPort COM;
        private int BoxAddress;
        private MyComDelegate ComPackFinishEvent;
        private byte[] rs_receive_buffer;
        private byte rs_receive_len;
        private bool rs_receive_flag;
        private ShowSendDataDel showSend;
        byte slave = 0;

        public BOX20(SerialPort com, int addr, MyComDelegate func)
        {
            this.BoxAddress = 0;
            this.rs_receive_buffer = new byte[50];
            this.rs_receive_len = 0;
            this.rs_receive_flag = false;
            this.COM = com;
            this.BoxAddress = addr;
            slave = (byte)addr;
            this.COM.DataReceived += new SerialDataReceivedEventHandler(this.COM_DataReceived);
            this.ComPackFinishEvent = func;
        }

        public BOX20(SerialPort com, int addr, MyComDelegate func, ShowSendDataDel del)
        {
            this.BoxAddress = 0;
            this.rs_receive_buffer = new byte[50];
            this.rs_receive_len = 0;
            this.rs_receive_flag = false;
            this.COM = com;
            this.BoxAddress = addr;
            slave = (byte)addr;
            this.COM.DataReceived += new SerialDataReceivedEventHandler(this.COM_DataReceived);
            this.ComPackFinishEvent = func;
            this.showSend = del;
        }

        //01 80 01 AA 2C 0D 0A 
        //01 80 01 55 D7 0D 0A
        public bool Check()
        {
            byte[] cmd_buffer = new byte[0x20];
            byte cmd_len = 0;
            byte[] data_buffer = new byte[0x20];
            data_buffer[0] = 170;
            //PackDataToBuffer(ref cmd_buffer, ref cmd_len, 0, 128, 1, data_buffer);
            PackDataToBuffer(ref cmd_buffer, ref cmd_len, slave, 128, 1, data_buffer);
            ClearComStuff();

            return false;
        }

        public bool ChargeAll()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 2, 0, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.DelayMs(50);
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(300);
        }

        private void ClearComStuff()
        {
            if ((this.COM != null) && this.COM.IsOpen)
            {
                this.COM.DiscardInBuffer();
                this.rs_receive_len = 0;
                this.rs_receive_flag = false;
            }
        }

        private void COM_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int bytesToRead = this.COM.BytesToRead; 
                int num2 = this.COM.Read(this.rs_receive_buffer, this.rs_receive_len, bytesToRead);
                this.rs_receive_len = (byte)(this.rs_receive_len + ((byte)num2));
                for (int i = 0; i < this.rs_receive_len; i++)
                {
                    if (i >= this.rs_receive_buffer.Length) break;
                    if (((i > 2) && (this.rs_receive_buffer[i] == 10)) && (this.rs_receive_buffer[i - 1] == 13))
                    {
                        this.rs_receive_flag = true;
                        if (this.ComPackFinishEvent != null)
                        {
                            this.ComPackFinishEvent(this.rs_receive_buffer, i + 1);
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }

        }

        private void DelayMs(int dly)
        {
            int num = dly / 10;
            for (int i = 0; i < num; i++)
            {
                Thread.Sleep(10);
                Application.DoEvents();
            }
        }

        public bool DischargeAll()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 7, 0, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.DelayMs(50);
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(500);
        }

        public bool GetVersion()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 0x81, 0, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(300);
        }

        public bool HandShake()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 0x80, 1, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(300);
        }

        public bool InitialAll()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 1, 0, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.DelayMs(50);
            this.COM.Write(buffer, 0, num);
            bool flag = false;
            if (this.WaitAndGetState(300))
            {
                flag = true;
            }
            return flag;
        }

        public bool LCRTestChannel(byte channel, bool on)
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = channel;
            if (on)
            {
                PackDataToBuffer(ref buffer, ref num, slave, 11, 1, buffer2);
            }
            else
            {
                PackDataToBuffer(ref buffer, ref num, slave, 12, 1, buffer2);
            }
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(500);
        }

        public bool LCTestAll()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 0x10, 0, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.DelayMs(50);
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(300);
        }

        public bool LcTestChannel(byte channel, bool on)
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = channel;
            if (on)
            {
                PackDataToBuffer(ref buffer, ref num, slave, 5, 1, buffer2);
            }
            else
            {
                PackDataToBuffer(ref buffer, ref num, slave, 6, 1, buffer2);
            }
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(500);
        }

        public bool SwitchToLCR()
        {
            byte[] buffer = new byte[0x20];
            byte num = 0;
            byte[] buffer2 = new byte[0x20];
            buffer2[0] = 170;
            PackDataToBuffer(ref buffer, ref num, slave, 10, 0, buffer2);
            if (this.showSend != null)
            {
                this.showSend(buffer, num);
            }
            this.ClearComStuff();
            this.COM.Write(buffer, 0, num);
            return this.WaitAndGetState(500);
        }

        public bool WaitAndGetState(int dly) => 
            true;

        public byte[] ReceivedBuffer
        {
            get ;
            set ;
        }

        public byte ReceivedLength { get { return this.rs_receive_len; } }
        public void PackDataToBuffer(ref byte[] Buffer, ref byte total_len, byte addr, byte cmd, byte data_len, byte[] data_list)
        {
            int index = 0;
            Buffer[0] = addr;
            Buffer[1] = cmd;
            Buffer[2] = data_len;
            index = 0;
            while (index < data_len)
            {
                Buffer[3 + index] = data_list[index];
                index++;
            }
            byte num2 = 0;
            for (int i = 0; i < (3 + index); i++)
            {
                num2 = (byte)(num2 + Buffer[i]);
            }
            Buffer[3 + index] = num2;
            Buffer[4 + index] = 13;
            Buffer[5 + index] = 10;
            total_len = (byte)(6 + index);
        }

        public void UnPackData(ref PackClass pack, byte[] src, byte len)
        {
            if (src[0] == 0)
            {
                for (int i = 0; i < len; i++)
                {
                    if (((i >= 2) && (src[i] == 10)) && (src[i - 1] == 13))
                    {
                        pack.cmd = src[1];
                        pack.data_len = src[2];
                        for (int j = 0; j < src[2]; j++)
                        {
                            pack.data_list[j] = src[3 + j];
                        }
                    }
                }
            }
        }
    }
}

